const dao = require('./base');

module.exports = {

    mspList: async function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from afc_msp where del_flag=0`;
        let pre = [];
        if (condition.msp !== undefined && condition.msp !== '') {
            sql += ` and msp like %?%`;
            pre.push(condition.msp);
        }
        if (condition.del_flag !== undefined && condition.del_flag !== '') {
            sql += ` and del_flag = ?`;
            pre.push(condition.del_flag);
        }

        sql = dao.by(sql, by)

        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;

        let data;
        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    mspAdd: async function (fields = []) {
        let sql = "insert into afc_msp (msp) values(?)";
        let pre = [];
        let data;
        pre.push(fields.msp);
        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    mspUpdate: async function (condition = {}, fields = {}) {
        let sql = "update afc_msp set ";
        let pre = [];
        let data;
        if (fields.msp !== undefined && fields.msp !== '') {
            sql = dao.update_sql_splice(sql, "msp");
            pre.push(fields.msp);
        }
        if (fields.del_flag !== undefined && fields.del_flag !== '') {
            sql = dao.update_sql_splice(sql, "del_flag");
            pre.push(fields.del_flag);
        }
        if (condition.mid !== undefined && condition.mid !== '') {
            sql += ` where mid = ?`;
            pre.push(condition.mid);
        } else {
            return;
        }

        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },


};
